// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Stderr.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Usings

    using System;

    #endregion

    /// <summary>
    /// The timings.
    /// </summary>
    public class Stderr
    {
        #region Public Methods

        /// <summary>
        /// The write.
        /// </summary>
        /// <param name="format">
        /// The format.
        /// </param>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        public static void Write(string format, params object[] parameters)
        {
            Singleton<QueueBackgroundWorker>.Instance.Enqueue(new WriteTask(format, parameters));
        }

        /// <summary>
        /// The write.
        /// </summary>
        public static void Write()
        {
            Singleton<QueueBackgroundWorker>.Instance.Enqueue(new WriteTask(string.Empty, null));
        }

        /// <summary>
        /// The write.
        /// </summary>
        /// <param name="o">
        /// The o.
        /// </param>
        public static void Write(object o)
        {
            Singleton<QueueBackgroundWorker>.Instance.Enqueue(new WriteTask("{0}", o.ToString()));
        }

        /// <summary>
        /// The benchmark.
        /// </summary>
        /// <param name="format">
        /// The format.
        /// </param>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        public static void WriteLine(string format, params object[] parameters)
        {
            Singleton<QueueBackgroundWorker>.Instance.Enqueue(new WriteLineTask(format, parameters));
        }

        /// <summary>
        /// The write line.
        /// </summary>
        public static void WriteLine()
        {
            Singleton<QueueBackgroundWorker>.Instance.Enqueue(new WriteLineTask(string.Empty, null));
        }

        /// <summary>
        /// The write line.
        /// </summary>
        /// <param name="o">
        /// The o.
        /// </param>
        public static void WriteLine(object o)
        {
            Singleton<QueueBackgroundWorker>.Instance.Enqueue(new WriteLineTask("{0}", o.ToString()));
        }

        #endregion

        /// <summary>
        /// The write task.
        /// </summary>
        internal class WriteLineTask : QueueBackgroundWorker.Task
        {
            #region Constants and Fields

            /// <summary>
            /// The format.
            /// </summary>
            private string format;

            /// <summary>
            /// The parameters.
            /// </summary>
            private object[] parameters;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="WriteLineTask"/> class.
            /// </summary>
            /// <param name="format">
            /// The format.
            /// </param>
            /// <param name="parameters">
            /// The parameters.
            /// </param>
            public WriteLineTask(string format, params object[] parameters)
            {
                this.format = format;
                this.parameters = parameters;
                if (this.parameters == null)
                {
                    this.parameters = new object[] { };
                }
            }

            #endregion

            #region Properties

            /// <summary>
            /// Gets Name.
            /// </summary>
            public override string Name
            {
                get
                {
                    return "WriteLineTask";
                }
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The run.
            /// </summary>
            public override void Run()
            {
                Console.Error.WriteLine(this.format, this.parameters);
            }

            #endregion
        }

        /// <summary>
        /// The write task.
        /// </summary>
        internal class WriteTask : QueueBackgroundWorker.Task
        {
            #region Constants and Fields

            /// <summary>
            /// The format.
            /// </summary>
            private string format;

            /// <summary>
            /// The parameters.
            /// </summary>
            private object[] parameters;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="WriteTask"/> class. 
            /// Initializes a new instance of the <see cref="WriteLineTask"/> class.
            /// </summary>
            /// <param name="format">
            /// The format.
            /// </param>
            /// <param name="parameters">
            /// The parameters.
            /// </param>
            public WriteTask(string format, params object[] parameters)
            {
                this.format = format;
                this.parameters = parameters;
                if (this.parameters == null)
                {
                    this.parameters = new object[] { };
                }
            }

            #endregion

            #region Properties

            /// <summary>
            /// Gets Name.
            /// </summary>
            public override string Name
            {
                get
                {
                    return "WriteLineTask";
                }
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The run.
            /// </summary>
            public override void Run()
            {
                Console.Error.Write(this.format, this.parameters);
            }

            #endregion
        }
    }
}